﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Forms
{
    public partial class report : Form
    {
        private eCompany.eObj.currency_list m_cl = null;
        private eCompany.eObj.organisation m_org = null;

        public report(eCompany.eObj.organisation org, eCompany.eObj.currency_list cl)
        {
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;

            m_cl = cl;
            m_org = org;
            InitReportList();
        }

        private void InitReportList()
        {
            reportComboBox.Items.Add("Day Report");
            reportComboBox.Items.Add("Week Report");
            reportComboBox.Items.Add("Month Report");
        }

        private void reportByDate(DateTime date_deb, DateTime date_fin, DateTime loop_end_date_deb, string loop_type)
        {
            reportListView.Visible = false;
            reportListView.BeginUpdate();

            reportListView.Columns.Clear();
            reportListView.Items.Clear();

            reportChart.Legends.Clear();
            reportChart.Series.Clear();

            System.Windows.Forms.DataVisualization.Charting.Legend legend = reportChart.Legends.Add("LEGEND");
            legend.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;

            System.Windows.Forms.DataVisualization.Charting.Series total_serie = reportChart.Series.Add("TOTAL");
            total_serie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            total_serie.ToolTip = "#VAL{N2}";
            total_serie.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Diamond;
            total_serie.MarkerSize = 5;
            total_serie.BorderWidth = 2;

            System.Windows.Forms.DataVisualization.Charting.Series org_serie = reportChart.Series.Add(m_org.name);
            org_serie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            org_serie.ToolTip = "#VAL{N2}";
            org_serie.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;

            foreach (eCompany.eObj.company c in m_org.company_list)
            {
                System.Windows.Forms.DataVisualization.Charting.Series serie = reportChart.Series.Add(c.name);
                serie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                serie.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
                serie.ToolTip = "#VAL{N2}";
            }

            ColumnHeader header = new ColumnHeader();
            header.Text = "Company";
            header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            reportListView.Columns.Add(header);

            header = new ColumnHeader();
            header.Text = "Profit Local";
            header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            header.TextAlign = HorizontalAlignment.Right;
            reportListView.Columns.Add(header);

            header = new ColumnHeader();
            header.Text = "Profit Gold";
            header.TextAlign = HorizontalAlignment.Right;
            header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            reportListView.Columns.Add(header);

            eCompany.eManager.eManager eM = new eManager.eManager();

            while (date_deb >= loop_end_date_deb)
            {
                double total_gold = 0;
                ListViewGroup grp = new ListViewGroup();
                grp.Header = date_deb.ToString("m") + " to " + date_fin.ToString("m");
                reportListView.Groups.Add(grp);

                ListViewItem org_item = new ListViewItem();
                org_item.Text = m_org.name;
                org_item.Group = grp;
                org_item.Font = new System.Drawing.Font(org_item.Font.FontFamily, org_item.Font.Size, FontStyle.Bold);

                reportListView.Items.Add(org_item);
                double org_gold = 0;
                foreach (eCompany.eObj.account acc in m_org.account_list)
                {
                    ListViewItem item = new ListViewItem();
                    item.Group = grp;
                    item.Text = "    " + acc.name;

                    double local = eM.getAccountValue(acc, date_deb, date_fin);
                    double gold = local;
                    if (acc.currency != "GLD")
                        gold = eM.currencyToGOLD(m_cl, acc.currency, local);

                    total_gold += gold;
                    org_gold += gold;
                    item.SubItems.Add(eCompany.eManager.eManager.formatDouble(local) + acc.currency);
                    item.SubItems.Add(eCompany.eManager.eManager.formatDouble(gold) + "G");
                    reportListView.Items.Add(item);
                }
                org_item.SubItems.Add("");
                org_item.SubItems.Add(eManager.eManager.formatDouble(org_gold) + "G");
                org_serie.Points.AddXY(date_deb, org_gold);

                foreach (eCompany.eObj.company c in m_org.company_list)
                {
                    System.Windows.Forms.DataVisualization.Charting.Series comp_serie = reportChart.Series[c.name];

                    ListViewItem comp_item = new ListViewItem();
                    comp_item.Group = grp;
                    comp_item.Text = c.name;
                    comp_item.Font = new System.Drawing.Font(comp_item.Font.FontFamily, comp_item.Font.Size, FontStyle.Bold);
                    reportListView.Items.Add(comp_item);
                    double comp_gold = 0;

                    foreach (eCompany.eObj.account acc in c.account_list)
                    {
                        ListViewItem item = new ListViewItem();
                        item.Group = grp;
                        item.Text = "    " + acc.name;

                        double local = eM.getAccountValue(acc, date_deb, date_fin);
                        double gold = local;

                        if (acc.currency != "GLD")
                            gold = eM.currencyToGOLD(m_cl, acc.currency, local);

                        total_gold += gold;
                        comp_gold += gold;
                        item.SubItems.Add(eCompany.eManager.eManager.formatDouble(local) + acc.currency);
                        item.SubItems.Add(eCompany.eManager.eManager.formatDouble(gold) + "G");
                        reportListView.Items.Add(item);
                    }
                    comp_item.SubItems.Add("");
                    comp_item.SubItems.Add(eManager.eManager.formatDouble(comp_gold) + "G");
                    comp_serie.Points.AddXY(date_deb, comp_gold);
                }

                grp.Header = grp.Header + "      (profit = " + eManager.eManager.formatDouble(total_gold) + "G)";
                total_serie.Points.AddXY(date_deb, total_gold);
                if (loop_type == "month")
                {
                    date_fin = date_deb.AddTicks(-1);
                    date_deb = date_deb.AddMonths(-1);
                }
                else if (loop_type == "week")
                {
                    date_deb = date_deb.AddDays(-7);
                    date_fin = date_deb.AddDays(7).AddTicks(-1);
                }
                else if (loop_type == "day")
                {
                    date_fin = date_deb.AddTicks(-1);
                    date_deb = date_deb.AddDays(-1);
                }
            }

            foreach (ColumnHeader h in reportListView.Columns)
            {
                h.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
            reportListView.SetGroupState(ListViewGroupState.Collapsed|ListViewGroupState.Collapsible);
            reportListView.EndUpdate();
            reportListView.Visible = true;
        }

        private double getDayOfWeekIndex(DayOfWeek dow)
        {
            switch (dow)
            {
                case DayOfWeek.Friday:
                    return 5;
                case DayOfWeek.Monday:
                    return 0;
                case DayOfWeek.Saturday:
                    return 6;
                case DayOfWeek.Sunday:
                    return 7;
                case DayOfWeek.Thursday:
                    return 4;
                case DayOfWeek.Tuesday:
                    return 1;
                case DayOfWeek.Wednesday:
                    return 3;
                default:
                    return 0;
            }
        }

        private void reportComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (reportComboBox.Text == "Week Report")
            {
                DateTime week_deb = DateTime.Today.AddDays(-getDayOfWeekIndex(DateTime.Today.DayOfWeek));
                DateTime week_fin = week_deb.AddDays(7).AddTicks(-1);
                DateTime loop_end_deb_date = week_deb.AddDays(-7 * 5);
                reportByDate(week_deb, week_fin, loop_end_deb_date, "week");
            }
            else if (reportComboBox.Text == "Month Report")
            {
                DateTime date_deb = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                DateTime date_fin = date_deb.AddMonths(1).AddTicks(-1);
                DateTime loop_end_deb_date = date_deb.AddMonths(-6);
                reportByDate(date_deb, date_fin, loop_end_deb_date, "month");            
            }
            else if (reportComboBox.Text == "Day Report")
            {
                DateTime date_deb = DateTime.Today;
                DateTime date_fin = date_deb.AddDays(1).AddTicks(-1);
                DateTime loop_end_deb_date = date_deb.AddDays(-30);
                reportByDate(date_deb, date_fin, loop_end_deb_date, "day");
            }
        }
    }
}
